﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using park.Common;


namespace park.Equipment
{
    public partial class MeteredEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPayStrategy();
                LoadPageInfo();
            }
        }

        private void LoadPayStrategy()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

             string strJson = sl.ListPayStrategy();
            //string strJson = "{ListPayStrategy:[{id:1,Strategy:\"收费策略1\"},{id:2,Strategy:\"收费策略2\"}]}";

            JObject obj = JObject.Parse(strJson);

            for (int i = 0; i < obj["ListPayStrategy"].Count(); i++)
            {
                ListItem li = new ListItem(obj["ListPayStrategy"][i]["Strategy"].ToString(), obj["ListPayStrategy"][i]["id"].ToString());
                this.ddlPayStrategy.Items.Add(li);
            }

        }


        private void LoadPageInfo()
        {
            if (Request.QueryString["meteredId"] != null)
            {
                int id = int.Parse(Request.QueryString["meteredId"]);
                cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
                string strJson = sl.LoadMeteredByID(id);

                //string strJson = ""
                //    + "{"
                //    + "\"MeteredId\":1,"
                //    + "\"CityId\":2,"
                //    + "\"AreaId\":2001,"
                //    + "\"StreetId\":24,"
                //    + "\"MeteredNo\":\"TSM4516592\","
                //    + "\"MeteredCode\":\"MCODE001\","
                //    + "\"MeteredName\":\"咪表名称\","
                //    + "\"Longitude\":\"15.25\","
                //    + "\"Latitude\":\"66.44\","
                //    + "\"InducedScreenNo\":\"SCREENNO001\","
                //    + "\"Connection\":\"18900988782\","
                //    + "\"PricingStrategy\":\"收费策略2\","
                //    + "\"InstallTime\":\"2012-07-22 23:20:14\""
                //    + "}";
                JObject obj = JObject.Parse(strJson);

                this.txtMeteredNo.Text = obj["MeteredNo"].ToString();
                this.txtMeteredCode.Text = obj["MeteredCode"].ToString();
                this.txtMeteredName.Text = obj["MeteredName"].ToString();
                this.txtLongitude.Text = obj["Longitude"].ToString();
                this.txtLatitude.Text = obj["Latitude"].ToString();
                this.txtInducedScreen.Text = obj["InducedScreenNo"].ToString();
                this.txtConnection.Text = obj["Connection"].ToString();
                this.txtInstallTime.Text = obj["InstallTime"].ToString();
                this.hdfID.Value = obj["MeteredId"].ToString();

                try
                {
                    this.ddlPayStrategy.Items.FindByText(obj["PricingStrategy"].ToString()).Selected = true;
                }
                catch { }

                Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script type=\"text/javascript\">var cityId = \"" + obj["CityId"].ToString() + "\"; var areaId = \"" + obj["AreaId"].ToString() + "\";var streetId = \"" + obj["StreetId"].ToString() + "\";</script>");
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int cityId = (Utils.IsInt(Request.Form["ddl_City"]) ? int.Parse(Request.Form["ddl_City"]) : 0);
            int areaId = (Utils.IsInt(Request.Form["ddl_Area"]) ? int.Parse(Request.Form["ddl_Area"]) : 0);
            int streetId = (Utils.IsInt(Request.Form["ddl_Street"]) ? int.Parse(Request.Form["ddl_Street"]) : 0);
            int id = int.Parse(this.hdfID.Value);

            string meteredNo = this.txtMeteredNo.Text;
            string meteredCode = this.txtMeteredCode.Text;
            string meteredName = this.txtMeteredName.Text;
            string longitude = this.txtLongitude.Text;
            string latitude = this.txtLatitude.Text;
            string inducedScreenNo = this.txtInducedScreen.Text;
            string connection = this.txtConnection.Text;
            string pricingStrategy = this.ddlPayStrategy.SelectedValue;
            DateTime installTime = string.IsNullOrEmpty(this.txtInstallTime.Text) ? DateTime.Parse("1900-01-01") : DateTime.Parse(this.txtInstallTime.Text);


            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            bool flag = sl.UpdateMetered(id,cityId, areaId, streetId, meteredNo, meteredCode, meteredName, longitude, latitude, inducedScreenNo, connection, pricingStrategy, installTime);

            string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

            Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");

        }

    }
}